package com.gitee.huxing.course.domain.dao;

import com.gitee.huxing.course.domain.UserInfoDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.domain.dao
 * @ClassName: UserDAOImpl
 * @Author: huxing
 * @DateTime: 2021-09-05 上午9:01
 */
@Slf4j
@Service("userDao")
public class UserDAOImpl implements UserDAO{

    @Resource(name = "myJdbcTemplate")
    JdbcTemplate jdbcTemplate;
    @Override
    public List<UserInfoDO> list() {
        // 查询语句
        String sql = "select * from pl_user_info";
        // 查询列表记录
        final List<UserInfoDO> list = new ArrayList<>();
        // 批量查询处理
        jdbcTemplate.query(sql, resultSet -> {
            UserInfoDO userInfoDO = new UserInfoDO();
            userInfoDO.setUserId(resultSet.getString("user_id"));
            userInfoDO.setUserName(resultSet.getString("user_name"));
            userInfoDO.setUseTel(resultSet.getString("user_tel"));
            userInfoDO.setUseAddress(resultSet.getString("user_address"));
            userInfoDO.setCreateBy(resultSet.getString("create_by"));
            userInfoDO.setCreateDate(resultSet.getDate("create_date"));
            userInfoDO.setUpdateBy(resultSet.getString("update_by"));
            userInfoDO.setUpdateDate(resultSet.getDate("update_date"));
            userInfoDO.setDeleteFlag(resultSet.getInt("delete_flag"));
            userInfoDO.setRemark(resultSet.getString("remark"));
            // 添加结果集
            list.add(userInfoDO);
        });
        // 检查返回的结果是否正确
        if (list != null && list.size() > 0){
            log.info("查询到的记录: {}", list.toString());
            return list;
        } else {
            return null;
        }
    }

    @Override
    public UserInfoDO selectByName(String userId) {
        // 查询语句
        String sql = "select * from pl_user_info where user_id = ?";
        // 查询记录
        UserInfoDO userInfoDO = jdbcTemplate.queryForObject(sql, (resultSet, i) -> {
            // 处理返回的记过值
            UserInfoDO infoDO = new UserInfoDO();
            infoDO.setUserId(resultSet.getString("user_id"));
            infoDO.setUserName(resultSet.getString("user_name"));
            infoDO.setUseTel(resultSet.getString("user_tel"));
            infoDO.setUseAddress(resultSet.getString("user_address"));
            infoDO.setCreateBy(resultSet.getString("create_by"));
            infoDO.setCreateDate(resultSet.getDate("create_date"));
            infoDO.setUpdateBy(resultSet.getString("update_by"));
            infoDO.setUpdateDate(resultSet.getDate("update_date"));
            infoDO.setDeleteFlag(resultSet.getInt("delete_flag"));
            infoDO.setRemark(resultSet.getString("remark"));
            return infoDO;
        }, userId);
        log.info("查询到的记录: {}", userInfoDO.toString());
        return userInfoDO;
    }
}
